/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						 adv_object_icon_type.cpp

	$Header: /heroes4/adv_object_icon_type.cpp $

	$NoKeywords: $

 ************************************************************************/

#include "precompiled.h"
#include "adv_object_icon_type.h"

#include <utility>

#include "adv_object_type.h"
#include "elements_of.h"
#include "materials.h"
#include "qualified_adv_object_type.h"

// --------------------------------------------------------------------------
// Unnamed namespace
// --------------------------------------------------------------------------

namespace
{

	// --------------------------------------------------------------------------
	// t_adv_object_type_info struct
	// --------------------------------------------------------------------------

	struct t_adv_object_type_info
	{
		t_adv_object_icon_type			icon_type;
		t_adv_object_icon_type const *	subtype_icon_types;

		t_adv_object_type_info();
	};

	inline t_adv_object_type_info::t_adv_object_type_info()
		:	icon_type( k_adv_object_icon_none ),
			subtype_icon_types( 0 )
	{
	}

	// --------------------------------------------------------------------------
	// t_adv_object_type_info_table type
	// --------------------------------------------------------------------------

	typedef t_adv_object_type_info t_adv_object_type_info_table[ k_adv_object_type_count ];

	// --------------------------------------------------------------------------
	// --------------------------------------------------------------------------
	t_adv_object_type_info_table const & get_adv_object_type_info_table()
	{
		static t_adv_object_type_info_table type_info_table;
		static bool initialized = false;
		if ( !initialized )
		{
			struct t_type_pair
			{
				t_adv_object_type		type;
				t_adv_object_icon_type	icon_type;
			};

			static t_type_pair const k_type_pairs[] =
			{
				{ k_adv_object_artifact, k_adv_object_icon_artifact },
				{ k_adv_object_creature_dwelling, k_adv_object_icon_creature_dwelling },
				{ k_adv_object_gateway, k_adv_object_icon_teleporter },
				{ k_adv_object_teleporter_entrance, k_adv_object_icon_teleporter },
				{ k_adv_object_teleporter_exit, k_adv_object_icon_teleporter },
				{ k_adv_object_subterranean_gate, k_adv_object_icon_subterranean_gate },
				{ k_adv_object_town, k_adv_object_icon_town },
			};

			int i;
			for ( i = 0; i < ELEMENTS_OF( k_type_pairs ); ++i )
			{
				t_type_pair const & pair = k_type_pairs[ i ];
				type_info_table[ pair.type ].icon_type = pair.icon_type;
			}

			static t_adv_object_icon_type const k_mine_subtype_icon_types[ k_material_count ] =
			{
				k_adv_object_icon_gold_mine,
				k_adv_object_icon_sawmill,
				k_adv_object_icon_ore_pit,
				k_adv_object_icon_crystal_mine,
				k_adv_object_icon_sulfur_mine,
				k_adv_object_icon_alchemists_lab,
				k_adv_object_icon_gem_mine,
			};

			type_info_table[ k_adv_object_mine ].subtype_icon_types = k_mine_subtype_icon_types;
			type_info_table[ k_adv_object_abandoned_mine ].subtype_icon_types = k_mine_subtype_icon_types;
			type_info_table[ k_adv_object_vein ].subtype_icon_types = k_mine_subtype_icon_types;

			static t_adv_object_icon_type const k_pile_subtype_icon_types[ k_material_count ] =
			{
				k_adv_object_icon_gold,
				k_adv_object_icon_wood,
				k_adv_object_icon_ore,
				k_adv_object_icon_crystal,
				k_adv_object_icon_sulfur,
				k_adv_object_icon_mercury,
				k_adv_object_icon_gems,
			};

			type_info_table[ k_adv_object_material_pile ].subtype_icon_types = k_pile_subtype_icon_types;

			initialized = true;
		}
		return type_info_table;
	}

} // Unnamed namespace

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
t_adv_object_icon_type get_icon_type( t_qualified_adv_object_type const & type )
{
	static t_adv_object_type_info_table const & k_type_info_table =
		get_adv_object_type_info_table();

	t_adv_object_type_info const & type_info = k_type_info_table[ type.get() ];
	if ( type_info.subtype_icon_types == 0 )
		return type_info.icon_type;

	return type_info.subtype_icon_types[ type.major_subtype() ];
}
